from collections import defaultdict, OrderedDict

# 通过collections的defaultdict方法 创建默认值int类型的字典
word_count_dict = defaultdict(int)
print(word_count_dict)

word_count_dict["apple"] += 1
print(word_count_dict["apple"])  # 1
print(word_count_dict["banana"])  # 0 (不会报错)


# 字典的合并，同名的key则被覆盖，类似js的Object.assign(),或者...
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4, "d": 5}

merged_dict = dict1 | dict2
print("merged_dict=", merged_dict)

dict1 = dict2 | dict1
print(dict1)  # 1,2,4,5

person = {"name": "jackie"}
print(person.get("age"))  # 没有的话返回None，不会报错，更安全

# print(person["age"])  # 通过这种访问字典，如果key不存在会报keyError的错误

name = person.pop("name")
print("删除的值为=", name)

try:
    age = person.pop("age")
except KeyError as e:
    print("删除错误=", e)

score = person.pop("score", "90")  # 删除score，如果score这个key不存在，就返回默认值90
print("score", score)
